import 'dart:ffi';

import 'package:customer/extension/extension_widget.dart';
import 'package:customer/theme.dart';
import 'package:fluent_ui/fluent_ui.dart';

import '../../db/entity/CustomerBillAttr.dart';
import '../../utils/common.dart';
import '../../widgets/gyy_theme.dart';

///订单下拉 显示控件
///订单产品 column

class BillProductDropPage extends StatefulWidget {

  final List<CustomerBillAttr> allAttrList;
  final bool readOnly;
  const BillProductDropPage(this.allAttrList ,{Key? key, this.readOnly = true})
      : super(key: key);

  @override
  State<BillProductDropPage> createState() => _EditProductItemState();
}

class _EditProductItemState extends State<BillProductDropPage> {

  late final List<CustomerBillAttr> allAttrList;
  Map<String, String> allAttrMap = {};
  bool isOpen = true;
  String productName = '';
  String price = '';

  initAttributeList() async {
    for (var element in allAttrList) {
      allAttrMap[element.attributeCode] = element.attributeName;
      if(element.attributeCode == 'price') {
        price = element.attributeValue;
      }
    }
    productName = allAttrList[0].productName;

    debugPrint('所有属性-----' + allAttrList.length.toString());
  }

  @override
  void initState() {
    super.initState();
    allAttrList = widget.allAttrList;
    initAttributeList();
  }

  @override
  Widget build(BuildContext context) {
    final theme = FluentTheme.of(context);
    return Expander(
      initiallyExpanded: isOpen,
      icon: Icon(isOpen ? FluentIcons.chevron_down : FluentIcons.chevron_up,
          size: 15, color: Colors.white),
      header: Text(productName,
        style: theme.typography.title!.merge(whiteStyle),
      ),
      headerBackgroundColor:
          ButtonState.all(AppTheme.accentColor.withOpacity(0.6)),
      trailing: Text(
        '小计:￥$price',
        style: theme.typography.bodyStrong!.merge(whiteStyle),
      ),
      onStateChanged: (open) => {
        setState(() => isOpen = open)
      },
      content: Column(
        children: [
          //扩展的属性
          ...buildWidgets(),
        ],
      ),
    ).padding(const EdgeInsets.all(10));
  }

  buildWidgets() {
    List<List<CustomerBillAttr>> oneList = split2CustomerBillAttr(allAttrList, 4);
    // debugPrint('多少行 ==：' + oneList.length.toString());
    List<Row> rows = []; //多少行
    int index = -1; //产品所有属性的角标
    for (var i = 0; i < oneList.length; i++) {
      List<CustomerBillAttr> twoList = oneList[i];
      // debugPrint('多少列 ==：' + twoList.length.toString());
      List<Widget> children = []; //每行的选项
      for (var j = 0; j < twoList.length; j++) {
        index++;
        // debugPrint('index==：' + index.toString());
        var e = twoList[j];
        //真实的数据
        if (e.billId != 0) {
          var name = e.attributeName;
          var value = e.attributeValue;
          children.add(
            Expanded(
                child: GyyTextBox(
                  header: name,
                  readOnly: widget.readOnly,
                  initialValue: value,
                  onChanged: (onChanged) {
                    // allInputAttrMap[code] = onChanged;
                    // widget.onProductAttrChanged(allInputAttrMap);
                  },
                )
            ),
          );
        } else {
          //最后一行 补齐的数据
          children.add(
            const Expanded(child: SizedBox()),
          );
        }
      }
      rows.add(Row(
        children: children,
      ));
    }
    return rows;
  }
}
